<jsp:useBean id="sessiondata" class="bibbletex.models.SessionData"
	scope="session" />

<%@ page
	import="bibbletex.models.Collaboration,bibbletex.models.Reference,bibbletex.models.SessionData"
	import="bibbletex.controllers.ReferenceController,bibbletex.helpers.ParseDetails"
	import="java.util.UUID,bibbletex.helpers.BibtexFormat, java.util.List" %>

<%
	Collaboration c = sessiondata.collaboration;
	Reference r = null;
	boolean AJAX = true;
	boolean failure = false;
	
	if (c != null && request.getHeader("referer").contains("import.jsp")){
		ParseDetails parser = new ParseDetails(request.getParameter("url"));
		List<Reference> refs = BibtexFormat.toReference(parser.getSite());
		if (refs.size() > 0)
			r = refs.get(0);
		else
			failure = true;
			
	}
	else if(c != null && request.getHeader("referer").contains("show.jsp")){
		String id_parameter = request.getParameter("rid");
		if (id_parameter != null)
			r = sessiondata.collaboration.getAllReferences().get(
					sessiondata.collaboration.getAllReferences().indexOf(
							new Reference(UUID.fromString(id_parameter))));
		else
			r = sessiondata.reference;
	}
	else if (c != null){
		AJAX = false;
		String id_parameter = request.getParameter("rid");
		if (id_parameter != null)
			r = sessiondata.collaboration.getAllReferences().get(
					sessiondata.collaboration.getAllReferences().indexOf(
							new Reference(UUID.fromString(id_parameter))));
		else
			r = sessiondata.reference;
	}
	if (!failure && c != null){
		r.setIs_suggestion(!sessiondata.collaboration_is_editable);
		ReferenceController.save(r);
		sessiondata.reference = r;
	
		// When from search
		if(c != null && AJAX)
			c.addReference(r);
	}
%>

<% if(!AJAX && !failure) { %>
<jsp:forward page="/collaboration/reference/show.jsp" />
<% } else if(!failure && c != null) { %>
<img src="/img/content/done.gif" />
<% } else { %>
<img src="/img/content/delete_hover.png" />
<% } %>
